<template>
	<div class="app-container">
		<view class="user-wrap">
			<image class="user-bg" src="/static/image/user/user-bg.jpg"></image>
			<view class="user-container" @click="jumpTo('/pages/user/detail')">
				<view class="user-avatar">
					<image class="user-avatar-image" :src="userInfo.avatar || '/static/image/user/avatar-default.png'"></image>
				</view>
				<view class="user-info">
					<text class="user-info-username">{{userInfo.username || '游客'}}</text>
				</view>
			</view>
			<view class="user-vip">
				<image class="user-vip-bg" src="/static/vip-card-bg.png" mode="scaleToFill"></image>
				<view class="user-vip-btn" @click="jumpTo('/pages/user/detail')">
					立即开通
				</view>
				<view class="user-vip-title">
					<text class="iconfont icon-vip"></text>
					浪亭会员
				</view>
				<text class="user-vip-member">浪亭 Union</text>
				<text class="user-vip-tip">开通会员开发无bug 一测就上线</text>
			</view>
		</view>
		<view 
			class="cover-wrap" 
			:style="[{ transform: coverTransform, transition: coverTransition }]" 
			@touchstart="coverTouchstart" 
			@touchmove="coverTouchmove" 
			@touchend="coverTouchend">

			<image class="cover-arc" src="/static/image/user/arc.png"></image>
			<!-- 统计 -->
			<view class="statistics-sction">
				<view class="item">
					<text class="num">128.8</text>
					<text>余额</text>
				</view>
				<view class="item">
					<text class="num">0</text>
					<text>优惠券</text>
				</view>
				<view class="item">
					<text class="num">20</text>
					<text>积分</text>
				</view>
			</view>
			<!-- 订单 -->
			<view class="order-section">
				<view class="item" @click="jumpTo('/pages/order/order?state=0')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="iconfont icon-order-all"></text>
					<text>全部订单</text>
				</view>
				<view class="item" @click="jumpTo('/pages/order/order?state=1')"  hover-class="common-hover" :hover-stay-time="50">
					<text class="iconfont icon-order-pay"></text>
					<text>待付款</text>
				</view>
				<view class="item" @click="jumpTo('/pages/order/order?state=2')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="iconfont icon-order-deliver"></text>
					<text>待收货</text>
				</view>
				<view class="item" @click="jumpTo('/pages/order/order?state=4')" hover-class="common-hover"  :hover-stay-time="50">
					<text class="iconfont icon-order-refund"></text>
					<text>退款/售后</text>
				</view>
			</view>
			<!-- 浏览记录 -->
			<view class="history-section">
				<view class="history-header">
					<text class="iconfont icon-history"></text>
					<text>浏览历史</text>
				</view>
				<scroll-view scroll-x class="history-list">
					<image @click="jumpTo('/pages/product/product')" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553105186633&di=c121a29beece4e14269948d990f9e720&imgtype=0&src=http%3A%2F%2Fimg004.hc360.cn%2Fm8%2FM04%2FDE%2FDE%2FwKhQplZ-QteEBvsbAAAAADUkobU751.jpg" mode="aspectFill"></image>
					<image @click="jumpTo('/pages/product/product')" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553105231218&di=09534b9833b5243296630e6d5b728eff&imgtype=0&src=http%3A%2F%2Fimg002.hc360.cn%2Fm1%2FM05%2FD1%2FAC%2FwKhQcFQ3iN2EQTo8AAAAAHQU6_8355.jpg" mode="aspectFill"></image>
					<image @click="jumpTo('/pages/product/product')" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553105320890&di=c743386be51f2c4c0fd4b75754d14f3c&imgtype=0&src=http%3A%2F%2Fimg007.hc360.cn%2Fhb%2FMTQ1OTg4ODY0MDA3Ny05OTQ4ODY1NDQ%3D.jpg" mode="aspectFill"></image>
					<image @click="jumpTo('/pages/product/product')" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2691146630,2165926318&fm=26&gp=0.jpg" mode="aspectFill"></image>
					<image @click="jumpTo('/pages/product/product')" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553105443324&di=8141bf13f3f208c61524d67f9bb83942&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01ac9a5548d29b0000019ae98e6d98.jpg" mode="aspectFill"></image>
					<image @click="jumpTo('/pages/product/product')" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=191678693,2701202375&fm=26&gp=0.jpg" mode="aspectFill"></image>
				</scroll-view>
			</view>
			<!-- 菜单列表 -->
			<view class="menu-section">
				<mix-list-cell icon="icon-money" iconColor="#e07472" title="我的钱包" tips="您的会员还有3天过期"></mix-list-cell>
				<mix-list-cell icon="icon-address" iconColor="#5fcda2" title="地址管理" @eventClick="jumpTo('/pages/address/index')"></mix-list-cell>
				<mix-list-cell icon="icon-share" iconColor="#9789f7" title="分享" tips="邀请好友赢10万大礼"></mix-list-cell>
				<mix-list-cell icon="icon-single" iconColor="#ee883b" title="晒单" tips="晒单抢红包"></mix-list-cell>
				<mix-list-cell icon="icon-collection" iconColor="#54b4ef" title="我的收藏"></mix-list-cell>
				<mix-list-cell icon="icon-setting" iconColor="#e07472" title="设置" border="" @eventClick="jumpTo('/pages/setting/index')"></mix-list-cell>
			</view>
		</view>
	</div>
</template>

<script>
	import { mapState } from 'vuex'
	import { showLoginDialog } from '@/utils/helper'
	let startY = 0, moveY = 0, pageAtTop = true
	
	export default {
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onShow() {
			this.initData()
		},
		methods: {
			// 初始化
			initData() {
				console.log('用户信息', this.hasLogin, this.userInfo)
				if (!this.hasLogin) {
					return false
				}
				this.$store.dispatch('getUserInfo')
			},
			// 跳转到...
			jumpTo(url) {
				if (!this.hasLogin) {
					return showLoginDialog()
				}
				this.$utils.jumpTo(url)
			},
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e){
				if(pageAtTop === false){
					return
				}
				this.coverTransition = 'transform .1s linear'
				startY = e.touches[0].clientY
			},
			coverTouchmove(e){
				moveY = e.touches[0].clientY
				let moveDistance = moveY - startY
				if(moveDistance < 0){
					this.moving = false
					return
				}
				this.moving = true
				if(moveDistance >= 80 && moveDistance < 100){
					moveDistance = 80
				}
					
				if(moveDistance > 0 && moveDistance <= 80){
					this.coverTransform = `translateY(${moveDistance}px)`
				}
			},
			coverTouchend(){
				if(this.moving === false){
					return
				}
				this.moving = false
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)'
				this.coverTransform = 'translateY(0px)'
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-wrap {
		position:relative;
		height: 520rpx;
		padding: 100rpx 30rpx 0;
		
		.user-bg {
			position:absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
		.user-container {
			position:relative;
			z-index: 1;
			display:flex;
			align-items:center;
			height: 180rpx;
			
			.user-avatar-image {
				width: 130rpx;
				height: 130rpx;
				border:5rpx solid #fff;
				border-radius: 50%;
			}
			.user-info {
				.user-info-username {
					margin-left: 20rpx;
					font-size: 32rpx;
					color: #333;
				}
			}
		}
		.user-vip {
			position: relative;
			display:flex;
			flex-direction: column;
			height: 240rpx;
			padding: 20rpx 24rpx;
			color: #f7d680;
			background: linear-gradient(left, rgba(0,0,0,.7), rgba(0,0,0,.8));
			border-radius: 16rpx 16rpx 0 0;
			overflow: hidden;
			
			.user-vip-bg {
				position:absolute;
				top: 20rpx;
				right: 0;
				width: 380rpx;
				height: 260rpx;
			}
			.user-vip-btn {
				position: absolute;
				z-index: 1;
				right: 20rpx;
				top: 16rpx;
				width: 132rpx;
				height: 40rpx;
				line-height: 40rpx;
				font-size: 22rpx;
				color: #36343c;
				text-align: center;
				background: linear-gradient(left, #f9e6af, #ffd465);
				border-radius: 20px;
			}
			.user-vip-title {
				margin-bottom: 28rpx;
				font-size: 28rpx;
				color: #f7d680;
				
				.iconfont{
					margin-right: 16rpx;
					color: #f6e5a3;
				}
			}
			.user-vip-tip {
				margin-top: 10rpx;
				font-size: 24rpx;
				color: #d8cba9;
			}
		}
	}
	.cover-wrap {
		position:relative;
		margin-top: -150rpx;
		padding: 0 30rpx;
		padding-bottom: 20rpx;
		background: #f5f5f5;
		.cover-arc {
			position:absolute;
			left: 0;
			top: -34rpx;
			width: 100%;
			height: 36rpx;
		}
		.statistics-sction {
			display:flex;
			justify-content: space-around;
			align-content: center;
			background: #fff;
			border-radius: 10rpx;
			
			.item{
				display:flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				height: 140upx;
				font-size: 24rpx;
				color: #75787d;
				
				.num{
					margin-bottom: 8upx;
					font-size: 32rpx;
					color: #333;
				}
			}
		}
		.order-section {
			display:flex;
			justify-content: space-around;
			align-content: center;
			padding: 28rpx 0;
			margin-top: 20rpx;
			background: #fff;
			border-radius: 10rpx;
			
			.item {
				display:flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 120rpx;
				height: 120rpx;
				font-size: 24rpx;
				color: #333;
				border-radius: 10rpx;
			}
			.iconfont {
				margin-bottom: 18rpx;
				font-size: 48rpx;
				color: #39b54a;
			}
		}
		.history-section {
			padding: 30rpx 0 20rpx;
			margin-top: 20rpx;
			background: #fff;
			border-radius:10rpx;
			
			.history-header {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #333;
				line-height: 40rpx;
				margin-left: 30rpx;
				
				.iconfont {
					font-size: 44rpx;
					color: #5eba8f;
					margin-right: 16rpx;
					line-height: 40rpx;
				}
			}
			.history-list {
				white-space: nowrap;
				padding: 30rpx 30rpx 0;
				image{
					display:inline-block;
					width: 160rpx;
					height: 160rpx;
					margin-right: 20rpx;
					border-radius: 10rpx;
				}
			}
		}
		.menu-section {
			padding: 30rpx 0 0;
			margin-top: 20rpx;
			background: #fff;
			border-radius:10rpx;
		}
	}
</style>